/*
	Copyright 2010 Anatol Gregory Mayen
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
 
	http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/
package eu.maydu.gwt.gxtcomponents.examples;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Anchor;

import eu.maydu.gwt.gxtcomponents.client.password.PasswordStrengthIndicator;
import eu.maydu.gwt.gxtcomponents.client.password.PasswordStrengthRule;
import eu.maydu.gwt.gxtcomponents.client.password.PasswordStrengthRule.RuleClass;

/**
 * Showcases the password strength indicator.
 * 
 * @author amayen
 *
 */
public class PasswordStrengthIndicatorExample extends FormPanel {
	
	private PasswordStrengthIndicatorExample self;

	public PasswordStrengthIndicatorExample() {
		super();
		setHeaderVisible(true);
		setHeading("Password strength indicator");
		TextField<String> passwordField = new TextField<String>();
		passwordField.setFieldLabel("Enter password");
		
		LayoutContainer infoLink = new LayoutContainer(new CenterLayout());
		Anchor a = new Anchor("Download password informational material");
		a.setHref("http://www.google.de/search?q=strong+passwords");
		a.setTarget("_blank");
		infoLink.add(a);
//		infoLink.setSize(400, 20);
//		infoLink.setBorders(true);
		
		PasswordStrengthIndicator indicator = new PasswordStrengthIndicator(passwordField, infoLink);
		
		//Alphabet rule
		PasswordStrengthRule rule = new PasswordStrengthRule();
		rule.setRuleClass(RuleClass.ALPHABET_IGNORECASE);
		rule.setRuleFulfilledCount(4);
		indicator.addPasswordStrengthRule(rule);
		
		//Numbers rule
		rule = new PasswordStrengthRule();
		rule.setRuleClass(RuleClass.NUMBERS);
		rule.setRuleFulfilledCount(2);
		indicator.addPasswordStrengthRule(rule);
		
		//Signs rule
		rule = new PasswordStrengthRule();
		rule.setRuleClass(RuleClass.SIGNS);
		rule.setRuleFulfilledCount(1);
		indicator.addPasswordStrengthRule(rule);
		
		//Length rule
		rule = new PasswordStrengthRule();
		rule.setRuleClass(RuleClass.PASSWORD_LENGTH);
		rule.setRuleFulfilledCount(8);
		indicator.addPasswordStrengthRule(rule);
		
		//Alphabet uppercase rule
		rule = new PasswordStrengthRule();
		rule.setRuleClass(RuleClass.ALPHABET_UPPERCASE);
		rule.setRuleFulfilledCount(2);
		indicator.addPasswordStrengthRule(rule);

		
		setSize(500, 400);
		setLabelWidth(150);
		add(indicator);
		add(passwordField);
		self = this;
		
		Button validateButton = new Button("Validate", new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				self.isValid();
			}
			
		});
		AdapterField buttonField = new AdapterField(validateButton);
		buttonField.setFieldLabel("");
		buttonField.setLabelSeparator("");
		add(buttonField);
	}
	
}
